package com.kchouse.mine;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.kchouse.MainActivity;
import com.kchouse.R;
import com.kchouse.bean.MinePrestoreItem;
import com.kchouse.bean.PrestoreRecordItem;
import com.kchouse.common.API;
import com.kchouse.common.BaseActivity;
import com.kchouse.common.MyHttpCycleContext;
import com.kchouse.common.ToastUtils;

import java.util.ArrayList;

import cn.finalteam.okhttpfinal.BaseHttpRequestCallback;
import cn.finalteam.okhttpfinal.HttpRequest;
import cn.finalteam.okhttpfinal.RequestParams;

public class MineprestoreDetailsActivity extends BaseActivity implements View.OnClickListener {

    private MineprestoreDetailsActivity mContext;
    private TextView tv_title;
    private ImageView iv_back;
    private Intent intent;
    private ListView lv_item;
    private LvAdapter prestoreAdapter;
    private String merchantId;
    private ArrayList<PrestoreRecordItem> listprestore = new ArrayList();
    private String MerchantShortName;
    private TextView tv_MerchantShortName;
    private int totalPoint;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mineprestore_details);

        mContext = this;
        intiData();
        intiView();

    }

    private void intiData() {
        intent = getIntent();
        merchantId = intent.getStringExtra("merchantId");
        totalPoint = intent.getIntExtra("totalPoint",0);
        MerchantShortName = intent.getStringExtra("MerchantShortName");
        GetPrestoreRecord();
    }

    private void intiView() {
        tv_title = (TextView) findViewById(R.id.tv_title);
        tv_title.setText("我的预存");
        iv_back = (ImageView) findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);

        tv_MerchantShortName = (TextView) findViewById(R.id.tv_MerchantShortName);
        tv_MerchantShortName.setText(MerchantShortName+"预存金额共："+totalPoint+"元");
        lv_item = (ListView) findViewById(R.id.lv_item);

        prestoreAdapter = new LvAdapter();
        lv_item.setAdapter(prestoreAdapter);
    }


    /**
     * 商户的预存消费记录
     */
    private void GetPrestoreRecord() {
        String url = API.GetPrestoreRecord;
        RequestParams params = new RequestParams(new MyHttpCycleContext());//请求参数
        params.addHeader("token", MainActivity.token);//添加header信息
        params.addFormDataPart("userId", MainActivity.userid);
        params.addFormDataPart("merchantId", merchantId);

        HttpRequest.get(url, params, new BaseHttpRequestCallback<String>() {

            //请求网络前
            @Override
            public void onStart() {
                showdialog();
            }

            @Override
            protected void onSuccess(String str) {
                Log.e("==", str);
                JSONObject json = JSONObject.parseObject(str);
                if (json.getBoolean("successful")) {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    listprestore.addAll(JSONObject.parseArray(data.getString("prestoreRecord"), PrestoreRecordItem.class));
                    prestoreAdapter.notifyDataSetChanged();
                } else {
                    String datastr = json.getString("data");
                    JSONObject data = JSONObject.parseObject(datastr);
                    ToastUtils.getInstanc(mContext).showToast(data.getString("msg"));
                }
            }

            //请求失败（服务返回非法JSON、服务器异常、网络异常）
            @Override
            public void onFailure(int errorCode, String msg) {
//                toast("网络异常~，请检查你的网络是否连接后再试");
                Log.e("==", msg);
            }

            //请求网络结束
            @Override
            public void onFinish() {
                dismissdialog();
            }
        });
    }


    class LvAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return listprestore.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PrestoreRecordItem item = listprestore.get(position);
            View view = getLayoutInflater().inflate(R.layout.lv_prestorerecord_item, null);
            TextView tv_detail = (TextView) view.findViewById(R.id.tv_detail);
            tv_detail.setText(item.getDetail());
            TextView tv_createTime = (TextView) view.findViewById(R.id.tv_createTime);
            tv_createTime.setText(item.getCreateTime());
            TextView tv_point = (TextView) view.findViewById(R.id.tv_point);
            if (item.getConsumeWay().equals("CONSUME"))//RECHARGE -充值，CONSUME -消费
            {
                tv_point.setText("-" + item.getPoint());
                tv_point.setTextColor(getResources().getColor(R.color.colorred));
            } else {
                tv_point.setText("+" + item.getPoint());
                tv_point.setTextColor(getResources().getColor(R.color.text_main));
            }
            TextView tv_carNumber = (TextView) view.findViewById(R.id.tv_carNumber);
            if (TextUtils.isEmpty(item.getCarNumber())) {
                tv_carNumber.setVisibility(View.GONE);
            } else {
                tv_carNumber.setText(item.getCarNumber() + "");
            }
            return view;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
